<?php
/**
 * Restore handling for resourcepage. 
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package package_name
 *//** */

// TODO This is not done at all yet!!

require_once('module_resourcepage.php');

//This function executes all the restore procedure about this mod
function resourcepage_restore_mods($mod,$restore) {

    global $CFG;

    $status = true;

    //Get record from backup_ids
    if($data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id)) {
        try {
            if (!defined('RESTORE_SILENTLY')) {
                $name=$data->info['MOD']['#']['NAME']['0']['#'];
                echo "<li>".get_string("modulename","resourcepage")." \"".htmlspecialchars($name)."\"</li>";
            }
            $newid=module_resourcepage::$inst->restore($restore,$data->info['MOD']['#'],
                restore_userdata_selected($restore,'resourcepage',$mod->id));
            backup_putid($restore->backup_unique_code,$mod->modtype,$mod->id, $newid);            
        } catch(Exception $e) {
            print '<pre>'.$e.'</pre>';
            $status=false;
        }
    }
    return $status;
}

//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//resourcepage_decode_content_links_caller() function in each module
//in the restore process
function resourcepage_decode_content_links ($content,$restore) {
    global $CFG;
        
    $result = $content;
            
    //Link to the list of resourcepages
    $searchstring='/\$@(RESOURCEPAGEINDEX)\*([0-9]+)@\$/';
    //We look for it
    $foundset=array();
    preg_match_all($searchstring,$content,$foundset);
    //If found, then we are going to look for its new id (in backup tables)
    if ($foundset[0]) {
        //print_object($foundset);                                     //Debug
        //Iterate over foundset[2]. They are the old_ids
        foreach($foundset[2] as $old_id) {
            //We get the needed variables here (course id)
            $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
            //Personalize the searchstring
            $searchstring='/\$@(RESOURCEPAGEINDEX)\*('.$old_id.')@\$/';
            //If it is a link to this course, update the link to its new location
            if(isset($rec->new_id)) {
                //Now replace it
                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/resourcepage/index.php?id='.$rec->new_id,$result);
            } else { 
                //It's a foreign link so leave it as original
                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/resourcepage/index.php?id='.$old_id,$result);
            }
        }
    }

    //Link to resourcepage view by moduleid
    $searchstring='/\$@(RESOURCEPAGEVIEWBYID)\*([0-9]+)@\$/';
    //We look for it
    preg_match_all($searchstring,$result,$foundset);
    //If found, then we are going to look for its new id (in backup tables)
    if ($foundset[0]) {
        //print_object($foundset);                                     //Debug
        //Iterate over foundset[2]. They are the old_ids
        foreach($foundset[2] as $old_id) {
            //We get the needed variables here (course_modules id)
            $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
            //Personalize the searchstring
            $searchstring='/\$@(RESOURCEPAGEVIEWBYID)\*('.$old_id.')@\$/';
            //If it is a link to this course, update the link to its new location
            if(isset($rec->new_id)) {
                //Now replace it
                $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/resourcepage/view.php?id='.$rec->new_id,$result);
            } else {
                //It's a foreign link so leave it as original
                $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/resourcepage/view.php?id='.$old_id,$result);
            }
        }
    }

    return $result;
}

//This function makes all the necessary calls to xxxx_decode_content_links()
//function in each module, passing them the desired contents to be decoded
//from backup format to destination site/course in order to mantain inter-activities
//working in the backup/restore process. It's called from restore_decode_content_links()
//function in restore process
function resourcepage_decode_content_links_caller($restore) {
    try {
        $newid=module_resourcepage::$inst->restore_decode_links($restore);
        return true;            
    } catch(Exception $e) {
        return false;
    }
}

//This function returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log.
function resourcepage_restore_logs($restore,$log) {
    try {
        return module_resourcepage::$inst->restore_log_entry($restore,$log);
    } catch(Exception $e) {
        return false;
    }
}
    
?>
